/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import envisage.Data.IArray;
	import envisage.Data.ITree;
	import envisage.Data.UIArrayEvent;
	
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	public class Tree extends List implements IArray
	{
		public function Tree()
		{
			super();
			
			DataArray = this;
		}
		
		public override function SetItemSelected(item:Object, selected:Boolean):void
		{
			super.SetItemSelected(item, selected);
			
			var tree:ITree = item as ITree;
			
			
			
			/*if( selected ) */SetTreeOpen(tree, ! IsTreeOpen(tree) );
			
			
			
		}
		
		private var displayRoot:Boolean = true;
		public function set DisplayRoot(value:Boolean):void
		{
			if( displayRoot != value )
			{
				displayRoot = value;
				//if( iArrayListener != null ) iArrayListener.ItemsReplaced();
				dispatchEvent(new UIArrayEvent(UIArrayEvent.ITEMS_REPLACED,0));
			}
		}
	
		private var iTree:ITree;
		public function get DataTree():ITree { return iTree; }
		public function set DataTree(value:ITree):void
		{
			if( iTree != value )
			{
				if( iTree != null ) {}
				iTree = value;
				if( iTree != null ) {}
				
				dictTreeToNVC = new Dictionary(true);
				//if( iArrayListener != null ) iArrayListener.ItemsReplaced();
				dispatchEvent(new UIArrayEvent(UIArrayEvent.ITEMS_REPLACED,0));
			}		
		}
		
		private var  dictTreeToNVC:Dictionary = new Dictionary(true);
		
		public function IsTreeOpen(iTree:ITree):Boolean
		{
			return dictTreeToNVC[iTree];
		}
				
		public function SetTreeOpen(iTree:ITree, open:Boolean):void
		{
			if( IsTreeOpen(iTree) == open ) return;
			
			var nvc:int;
			
			if( open == false )
			{
				nvc = -int(dictTreeToNVC[iTree]);
				delete dictTreeToNVC[iTree];
			}
			
			else
			{
				nvc = iTree.GetNumChildren();
				for(var i:int = 0 ; i < iTree.GetNumChildren() ; i++)
					nvc += int(dictTreeToNVC[iTree.GetChildItem(i)]);
					
				dictTreeToNVC[iTree] = nvc;
			}
			
			iTree = iTree.GetParent();
			while(iTree != null)
			{
				var n:int = int(dictTreeToNVC[iTree]) + nvc;
				if( n == 0 ) delete dictTreeToNVC[iTree];
				else dictTreeToNVC[iTree] = n;
				
				iTree = iTree.GetParent();
			}
			
			//if( iArrayListener != null ) iArrayListener.ItemsReplaced();		
			dispatchEvent(new UIArrayEvent(UIArrayEvent.ITEMS_REPLACED,0));
		}
		
		public function GetNumItems():int
		{
			if( displayRoot )
			{
				var i:int = int(dictTreeToNVC[iTree]);
				if( i == 0 )return 0;
				else return i+1; 
			}
			else return  int(dictTreeToNVC[iTree]);
		}
		
		private function TreeFromListIndex(index:int):ITree
		{
			var n:int = 0;
			var parent:ITree = iTree;
			
			if( displayRoot )
			{
				if( n == index ) return iTree;
				n++;
			}
			
			for(var i:int = 0 ; i < parent.GetNumChildren() ; i++)
			{
				var child:ITree = parent.GetChildItem(i);
				
				if( n == index ) return child;
				var nvc:int = int(dictTreeToNVC[child]);
				
				if( (index - n) <= nvc )
				{
					i = -1;
					parent = child;
					n++;
				}
				
				else n += (nvc + 1);
			}
		
			throw "";
		}
		
		public function GetItem(index:int):Object
		{
			return TreeFromListIndex(index);//.GetItem();
		}
		
		protected override function LayoutSizeChild(c:DisplayObject, index:int):void
		{
			super.LayoutSizeChild(c, index);
			
			var t:ITree = DataArray.GetItem(index) as ITree;
			
			var i:int = displayRoot? -1 : -2;
			while(t != null )
				{ t = t.GetParent(); i++; }
				
				c.x += 10 * i;
		}

	}
}

